package com.she.iiicoursecenter.activity.tab2_time;

import java.util.List;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.she.iiicoursecenter.activity.template.TemplateActivity;
import com.she.iiicoursecenter.event.base.Event;
import com.she.iiicoursecenter.event.connect.bean.SearchContentBean;
import com.she.iiicoursecenter.event.connect.event.SearchContentEvent;
import com.she.iiicoursecenter.event.iface.IEventCallback;
import com.she.iiicoursecenter.eventmanager.KenEventManager;
import com.she.iiicoursecenter.view.tab2.Tab2_CourseSelectedListView;

public class Tab2_CourseSelectedList extends TemplateActivity {
	//
	//
	private Tab2_CourseSelectedListView	mTab2_CourseSelectedListView;
	private ProgressDialog dialog ;
	
	public Tab2_CourseSelectedList() {
		// TODO Auto-generated constructor stub
	}

	private Handler notifyHandler = new Handler(){
		@Override
		public void dispatchMessage(Message msg) {
			// TODO Auto-generated method stub
			if (msg.what == 0) {
				mTab2_CourseSelectedListView.notifyAdapter();
			}else if(msg.what == 1){
				Toast.makeText(Tab2_CourseSelectedList.this, "查無符合條件的課程 請重新查詢", Toast.LENGTH_SHORT).show();
				mTab2_CourseSelectedListView.notifyAdapter();
			}
			
			dialog.cancel();
			super.dispatchMessage(msg);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		dialog = new ProgressDialog(this);
		 dialog.setMessage("讀取中...");
		 dialog.setIndeterminate(true);
		 dialog.setCancelable(true);
		 dialog.show();
		
		// get bundle from previous...
		Bundle bundle = this.getIntent().getExtras();
		// test only!!!
		String date = bundle.getString("date");
		String time = bundle.getString("time");
		String slot = bundle.getString("slot");
		
		mTab2_CourseSelectedListView = new Tab2_CourseSelectedListView(this);
		//
		setView(mTab2_CourseSelectedListView);
		setContentView(mTab2_CourseSelectedListView);		
		
		SearchContentEvent searchContentEvent = new SearchContentEvent(null, date, time, slot);
		searchContentEvent.addCallback(new IEventCallback() {
			
			public void result(Event event, Object result) {
				// TODO Auto-generated method stub
				if (((List<SearchContentBean>) result).size() >0) {
					getApp().setSearchContentData((List<SearchContentBean>) result);
					notifyHandler.sendEmptyMessage(0);
				}else{
					getApp().setSearchContentData((List<SearchContentBean>) result);
					notifyHandler.sendEmptyMessage(1);
				}

			}
			
			public void progress(Event event, int progress, String tmpPath) {
			}
		});
		KenEventManager.getInstance().execute(searchContentEvent);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
	}

	@Override
	public void onDetachedFromWindow() {
		// TODO Auto-generated method stub
		super.onDetachedFromWindow();
	}	
}
